<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

class b2c_ctl_admin_member_advancedetail extends desktop_controller{

    var $workground = 'b2c_ctl_admin_member';
    var $member_model ;

    public function __construct($app)
    {
        parent::__construct($app);
        $this->member_model = $this->app->model('member_shenhe');
        header("cache-control: no-store, no-cache, must-revalidate");
    }

    function index(){
        $actions_base['title'] = app::get('b2c')->_('预存款记录');
        $actions_base['use_buildin_recycle'] = false;
        // $actions_base['use_buildin_export'] = true;
        $actions_base['actions'] = array(
            array(
                'label' => app::get('ectools')->_('导出'),
                'target' => '_blank',
                'submit'=>'index.php?app=b2c&ctl=admin_member_advancedetail&act=deliverExport',
            ),
        );
        $this->finder('b2c_mdl_member_shenhe',$actions_base);
    }

     /**
     * 桌面订单相信汇总显示
     * @param null
     * @return null
     */
    public function _views(){
        $obj_shenhe = $this->app->model('member_shenhe');
        $sub_menu = array(
            1=>array('label'=>app::get('b2c')->_('审核完成'),'optional'=>false,'filter'=>array('shenhe_state'=>1,'disabled'=>'false')),
        );

        if(isset($_GET['optional_view'])) $sub_menu[$_GET['optional_view']]['optional'] = false;

        foreach($sub_menu as $k=>$v){
            if($v['optional']==false){
                $show_menu[$k] = $v;
                if(is_array($v['filter'])){
                    $v['filter'] = array_merge(array(),$v['filter']);
                }else{
                    $v['filter'] = array();
                }
                $show_menu[$k]['filter'] = $v['filter']?$v['filter']:null;
                if($k==$_GET['view']){
                    $show_menu[$k]['newcount'] = true;
                    $show_menu[$k]['addon'] = $obj_shenhe->count($v['filter']);
                }
                $show_menu[$k]['href'] = 'index.php?app=b2c&ctl=admin_member_advancedetail&act=index&view='.($k).(isset($_GET['optional_view'])?'&optional_view='.$_GET['optional_view'].'&view_from=dashboard':'');
            }elseif(($_GET['view_from']=='dashboard')&&$k==$_GET['view']){
                $show_menu[$k] = $v;
            }
        }
        // dump($show_menu);die;
        return $show_menu;
    }
    
    /**
    * ps ：导出
    * Time：2017/03/8 09:04:18
    * @author linsuchuan
    */
    public function deliverExport(){
        $arr[] = array(
            '序号',
            '用户名',
            '出入金额',
            '信用额度类型',
            '充值账户',
            '管理备注',
            '充值时间',
            '审核结果',
            '公司名称',
        );
        $objShenhe= app::get('b2c')->model('member_shenhe');
        $objMembers= app::get('b2c')->model('members');
        if ($_POST['isSelectedAll'] == '_ALL_') {
            $shenheList = $objShenhe->getList('*');
        }else{
            $shenhe_id = $_POST['shenhe_id'];
            $shenheList = $objShenhe->getList('*',array('shenhe_id'=>$shenhe_id));
        }
        foreach ($shenheList as $value) {
            $memberIds[] = $value['member_id'];
        }
        $memberIds = array_unique($memberIds);
        $companyNames = $objMembers->getList('member_id,name',array('member_id'=>$memberIds));
        foreach ($companyNames as $value) {
            $companyName[$value['member_id']] = $value['name'];
        }
        $xuhao = 1;
        foreach ($shenheList as $value) {
            if ($value['modify_advance_type']=='1') {
                $modify_advance_type = '实际预存';
            }elseif($value['modify_advance_type']=='0'){
                $modify_advance_type = '信用额度';
            }else{
                $modify_advance_type = '-';
            }
            if ($value['shenhe_state']=='1') {
                $shenhe_state = '审核通过';
            }elseif($value['shenhe_state']=='2'){
                $shenhe_state = '审核拒绝';
            }else{
                $shenhe_state = '审核中';
            }
            $arr[] = array(
                $xuhao,
                $value['login_account'],
                $value['modify_advance'],
                $modify_advance_type,
                $value['modify_bank_name'],
                $value['modify_memo'],
                date('Y-m-d H:i:s',$value['mtime']),
                $shenhe_state,
                $companyName[$value['member_id']],
            );
            $xuhao++;
        }
        $filetypeObj = kernel::single('importexport_type_xls');
        $filetypeObj->set_queue_header('depositLog.xls');
        $rs = $filetypeObj->fileHeader();
        foreach ($arr as $ref) {
            $rs .= $filetypeObj->arrToExportType(array($ref));
        }
        $rs .= $filetypeObj->fileFoot();
        if( method_exists($filetypeObj, 'setBom') ){
            $bom = $filetypeObj->setBom();
            echo $bom;
        }
        echo $rs;
    }
}
